//
//  VideoTableViewCell.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "VideoTableViewCell.h"
#import "DateBaseManager.h"

@interface VideoTableViewCell ()

@end
@implementation VideoTableViewCell
- (void)dealloc
{
    [_titleLable release];
    [_detailLable release];
    [_timeImg release];
    [_timeLable release];
    [_playImage release];
    [_playLable release];
    [_commentLable release];
    [_share release];
    [_likeBtn release];
    [_indexpath release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLable = [[UILabel alloc]init];
//        _titleLable.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_titleLable];
        [_titleLable release];
        
        self.detailLable = [[UILabel alloc]init];
//        _detailLable.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_detailLable];
        [_detailLable release];
        
        self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];//否则会有一个蓝色的条
        [self.contentView addSubview:_likeBtn];
        [_likeBtn release];
        
        self.timeImg = [[UIImageView alloc]init];
//        _timeImg.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_timeImg];
        [_timeImg release];
        
        self.timeLable = [[UILabel alloc]init];
//        _timeLable.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:_timeLable];
        [_timeLable release];
        
        self.playImage = [[UIImageView alloc]init];
//        _playImage.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:_playImage];
        [_playImage release];
        
        self.playLable = [[UILabel alloc]init];
//        _playLable.backgroundColor = [UIColor magentaColor];
        [self.contentView addSubview:_playLable];
        [_playLable release];
        
        self.commentLable = [[UILabel alloc]init];
//        _commentLable.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:_commentLable];
        [_commentLable release];
        
        self.share = [UIButton buttonWithType:UIButtonTypeSystem];
//        _share.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_share];
        
        self.myImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:_myImageView];
        
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    //title
    _titleLable.frame = CGRectMake(10, 10, self.contentView.frame.size.width - 20, self.contentView.frame.size.height / 10);
    //detail
    _detailLable.frame = CGRectMake(10, _titleLable.frame.origin.y + _titleLable.frame.size.height, _titleLable.frame.size.width, _titleLable.frame.size.height / 2);
    _detailLable.font = [UIFont systemFontOfSize:14];
    _detailLable.textColor = [UIColor grayColor];
    //likeBtn
    _likeBtn.frame = CGRectMake(self.contentView.frame.size.width - 30, _titleLable.frame.origin.y + 5, 20, 20);

    [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
    [_likeBtn addTarget:self action:@selector(likeAction) forControlEvents:UIControlEventTouchUpInside];
    //Imageview
    _myImageView.frame = CGRectMake(10, _detailLable.frame.origin.y + _detailLable.frame.size.height + 10, _detailLable.frame.size.width, self.contentView.frame.size.height / 3 * 2);
    _timeImg.image = [UIImage imageNamed:@"iconfont-clock"];
    //timeImage
    _timeImg.frame = CGRectMake(_myImageView.frame.origin.x, _myImageView.frame.size.height + _myImageView.frame.origin.y + 5, 20, 20);
    //timeLable
    self.timeLable.frame = CGRectMake(_timeImg.frame.origin.x + _timeImg.frame.size.width + 2, _timeImg.frame.origin.y, _timeImg.frame.size.width * 2, _timeImg.frame.size.height);
    _timeLable.textColor = [UIColor grayColor];
    _timeLable.font = [UIFont systemFontOfSize:10];
    //playImage
    self.playImage.frame = CGRectMake(_timeLable.frame.origin.x + _timeLable.frame.size.width + 10, _timeImg.frame.origin.y, _timeImg.frame.size.width, _timeImg.frame.size.height);
    _playImage.image = [UIImage imageNamed:@"iconfont-shipin"];
    //playLable
    self.playLable.frame = CGRectMake(_playImage.frame.origin.x + _playImage.frame.size.width + 2, _playImage.frame.origin.y, _timeLable.frame.size.width, _playImage.frame.size.height);
    _playLable.font = [UIFont systemFontOfSize:10];
    _playLable.textColor = [UIColor grayColor];
    //comment
    self.commentLable.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3, _timeImg.frame.origin.y, _timeLable.frame.size.width, _timeLable.frame.size.height);
    _commentLable.font = [UIFont systemFontOfSize:10];
    _commentLable.textColor = [UIColor grayColor];
    //shareBtn
    self.share.frame = CGRectMake(_commentLable.frame.origin.x + _commentLable.frame.size.width + 2, _timeImg.frame.origin.y, _timeLable.frame.size.height, _timeLable.frame.size.height);
    [_share setBackgroundImage:[UIImage imageNamed:@"iconfont-share"] forState:UIControlStateNormal];
     [_share addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
}
//收藏方法
-(void)likeAction
{
    //打开数据库
    DateBaseManager *dbManager = [DateBaseManager shareInstance];
    [dbManager openDB];
    //创建表
    [dbManager createTable];
    if (_likeBtn.selected) {
        //取消收藏
        NSMutableArray *arr = [dbManager select];
        Videomodel *m = arr[_indexpath.row];
        [dbManager deleteVideoWithId:m.videoId];
        [_likeBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-heart-2"] forState:UIControlStateNormal];
        NSLog(@"取消了收藏");
        }else{
            //收藏
            NSString *s = _model.title;
            NSLog(@"喜欢了1%@", s);
            //插入数据
            [dbManager insertVideo:_model];
            [_likeBtn setBackgroundImage:[UIImage imageNamed:@"collection.png"] forState:UIControlStateNormal];
             
        }
    _likeBtn.selected = !_likeBtn.selected;
    
    
}
-(void)shareAction;
{

    [self.delegate shareWithindex:_indexpath];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
